/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gu.derp.model;

import edu.gu.derp.database.Queries;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author c
 */
public class DatabaseController implements IController {
    
    private Queries query;
    private edu.gu.derp.database.Account dbAccount;
    
    public DatabaseController(Queries query){
        this.query = query;
    }

    @Override
    public boolean accountExists(String name) {
        return query.getUser(name) != null;
    }

    @Override
    public Account getAccount(String name) {
        dbAccount = query.getUser(name);
        return dbAccountToAccount(dbAccount);
    }

    @Override
    public void addAccount(Account account) {
        dbAccount = accountTodbAccount(account);
        query.addUser(dbAccount);
    }

    @Override
    public void removeAccount(Account account) {
        query.removeUser(account.getUsername());
    }

    @Override
    public void changePassword(Account account, String password) {
        query.changePassword(account.getUsername(), password);
    }

    @Override
    public void addFeed(Account account, Feed feed) {
        dbAccount = query.getUser(account.getUsername());
        query.linkUserFeed(dbAccount, feed.getUrl().toString());
    }

    @Override
    public void removeFeed(Account account, Feed feed) {
        dbAccount = query.getUser(account.getUsername());
        query.unlinkUserFeed(dbAccount, feed.getUrl().toString());    
    }

    @Override
    public List<Feed> getFeeds(Account account) {
        List<edu.gu.derp.database.Feed> dbFeeds = query.getUserFeeds(account.getUsername());
        List<Feed> feeds = new ArrayList<Feed>();
        for(edu.gu.derp.database.Feed feed : dbFeeds){
            feeds.add(dbFeedToFeed(feed));
        }
        return feeds;
    }
    
    
    /* 
     *  The following methods convert between the entity classes used in the 
     *  database and the corresponding classes used on the application side.
    */
    private Account dbAccountToAccount(edu.gu.derp.database.Account dbAccount){
        return new Account(dbAccount.getUsername(), dbAccount.getPassword(), dbAccount.getEmail());
    }
    
    private edu.gu.derp.database.Account accountTodbAccount(Account account){
        return new edu.gu.derp.database.Account(account.getUsername(), account.getPassword(), account.getEmail());
    }
    
    private Feed dbFeedToFeed(edu.gu.derp.database.Feed dbFeed){
        Feed feed = null;
        try {
            feed = Feed.fromURL(new URL(dbFeed.getUrl()));
        } catch (MalformedURLException ex) {
            Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return feed;
    }
    
    private edu.gu.derp.database.Feed feedTodbFeed(Feed feed){
        return new edu.gu.derp.database.Feed(feed.getUrl().toString());
    }
}
